/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tmp.ac.engin.gen;

import tmp.ac.engin.setting.usersSetting;
import tmp.ac.lib.tools.DBClass;
import ac.uitls.propertiesOperation;
import tmp.ac.lib.tools.uiUtil;
import tmp.ac.options.DataModel;
import java.sql.ResultSet;
import java.util.Arrays;

/**
 *
 * @author ismail
 */
public class genOprtions {

    public String[] Data;
    DBClass db = new DBClass();
    String[] myfileds;
    String mydatbe;
    propertiesOperation po = new propertiesOperation();

    public genOprtions(String mydatbe, String[] myfileds) {
        this.mydatbe = mydatbe;

        this.myfileds = myfileds;

    }

    public String addNew() {
        String returnData = "";

        usersSetting us = new usersSetting();

        if (us.IsCan("add")) {

            String[][] data = {myfileds, Data};
            db.setInsert(mydatbe, data);
            returnData = db.getErrorMessage();

            if (returnData.trim().equals("--")) {
                returnData = po.getWord("message.save");
            }
        } else {


            uiUtil.sendError(null, po.getWord("title.error"), po.getWord("message.addPerFailed"));

        }
        return returnData;
    }

    public String UpdateNew(int id) {
        String returnData = "";

        usersSetting us = new usersSetting();
        if (us.IsCan("edit")) {
            String[][] data = {myfileds, Data};
            db.setUpdate(mydatbe, data, id);

            returnData = db.getErrorMessage();
            if (returnData.trim().equals("--")) {
                returnData = po.getWord("message.update");
            }
        } else {


            uiUtil.sendError(null, po.getWord("title.error"), po.getWord("message.editPerFailed"));

        }
        return returnData;
    }

    public String getMax(String filed) {
        String returnData = db.getRow(mydatbe, "Max(`" + filed + "`) as morder", "1=1")[0];
        return returnData;
    }

    public ResultSet getAll() {
        db.setOpenCon();
        ResultSet rs = null;

        rs = db.getDataWithOutCon(mydatbe, "*", "1=1");


        return rs;
    }

    public ResultSet getAll(String flid, String value) {
        db.setOpenCon();
        ResultSet rs;

        if (flid.trim().equals("") || value.trim().equals("")) {
            rs = db.getDataWithOutCon(mydatbe, "*", "1=1");
        } else {
            rs = db.getDataWithOutCon(mydatbe, "*", "`" + flid + "`like'%" + value + "%'");



        }

        // uiUtil.sendMessage(null, "hg", db.getErrorMessage());

        return rs;
    }

    public String[] getRow(String flid, String value) {
        String data[] = db.getRow(mydatbe, "`" + flid + "`='" + value + "'");
        return data;
    }

    public String deleteCategory(int id) {

        usersSetting us = new usersSetting();
        String returnData = "";
        if (us.IsCan("delete")) {
            db.setDelete(mydatbe, id);
            returnData = db.getErrorMessage();
            if (returnData.trim().equals("--")) {
                returnData = po.getWord("message.deleteEnd");
            }
        } else {


            uiUtil.sendError(null, po.getWord("title.error"), po.getWord("message.deletePerFailed"));

        }

        return returnData;
    }
    //  model.insertRow(0,new Object[]{"Ranjan","50"});
}
